package com.appbee.cm.client.nav.presenter;

import javax.inject.Inject;

import com.appbee.cm.client.nav.text.NavText;
import com.appbee.cm.shared.data.NavItemProxy;
import com.appbee.cm.shared.data.NavItemRequest;
import com.appbee.cm.shared.dataservice.DataRequestFactory;
import com.appbee.core.client.event.EntityChange;
import com.appbee.core.client.msg.ui.IMessageView;
import com.appbee.core.client.ui.IConfirmView;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.web.bindery.requestfactory.gwt.client.RequestFactoryEditorDriver;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.google.web.bindery.requestfactory.shared.Request;
import com.google.web.bindery.requestfactory.shared.RequestFactory;
import com.google.web.bindery.requestfactory.shared.WriteOperation;

public class NavAdminPresenter
{
	private final View view;
	private final DataRequestFactory dataRequestFactory;
	private NavItemWrap selectedObject;
	private final EventBus eventBus;
	private RequestFactoryEditorDriver<NavItemProxy, ?> driver;
	private final NavControl navControl;
	private final IConfirmView confirmView;
	private final Scheduler scheduler;
	private final NavText navText;

	public interface View
	{
		void show();

		public void setPresenter(NavAdminPresenter presenter);

		public EditView getEditView();

		public EditView showEditView();

		public void hideEditView();
	}

	public interface EditView
	{
		public IMessageView getMessageView();

		public RequestFactoryEditorDriver<NavItemProxy, ?> createDriver(EventBus eventBus,
																		RequestFactory rf);
	}

	@Inject
	public NavAdminPresenter(	View view,
								IConfirmView confirmView,
								DataRequestFactory dataRequestFactory,
								EventBus eventBus,
								NavControl navControl,
								Scheduler scheduler,
								NavText navText)
	{
		this.view = view;
		this.confirmView = confirmView;
		this.dataRequestFactory = dataRequestFactory;
		this.eventBus = eventBus;
		this.navControl = navControl;
		this.scheduler = scheduler;
		this.navText = navText;
		view.setPresenter(this);

	}

	public void show()
	{
		view.hideEditView();
		view.show();
	}

	public void doNavItemSelected(NavItemWrap selectedObject)
	{
		view.showEditView().getMessageView().clearMessage();
		this.selectedObject = selectedObject;
		EditView editView = view.getEditView();
		driver = editView.createDriver(eventBus, dataRequestFactory);
		NavItemRequest navItemRequest = dataRequestFactory.getNavItemRequest();
		Request<NavItemProxy> save = navItemRequest.save(selectedObject
				.getWrappedItem());
		save.to(new UpdateReceiver());
		driver.edit(selectedObject.getWrappedItem(), navItemRequest);
	}

	public void doSave()
	{
		driver.flush().fire();
	}

	public void doDelete(final NavItemWrap navItemWrap)
	{
		if (!confirmView.confirm(navText.confirmDelete()))
			return;

		NavItemRequest navItemRequest = dataRequestFactory.getNavItemRequest();
		navItemRequest.delete(navItemWrap.getWrappedItem())
				.fire(new Receiver<Void>()
				{

					@Override
					public void onSuccess(Void response)
					{
						navControl.remove(navItemWrap);
					}
				});
	}

	public void doNew(NavItemWrap parent)
	{
		view.showEditView().getMessageView().clearMessage();
		EditView editView = view.getEditView();
		driver = editView.createDriver(eventBus, dataRequestFactory);
		NavItemRequest navItemRequest = dataRequestFactory.getNavItemRequest();
		NavItemProxy navItemProxy = navItemRequest.create(NavItemProxy.class);
		this.selectedObject = new NavItemWrap(navItemProxy);
		Request<NavItemProxy> save = navItemRequest.save(navItemProxy);
		save.to(new CreateReceiver());
		driver.edit(selectedObject.getWrappedItem(), navItemRequest);
		if (parent != null)
			navItemProxy.setParentId(parent.getWrappedItem().getId());

	}

	private class CreateReceiver extends SaveReceiver
	{
		@Override
		public void onSuccess(NavItemProxy response)
		{
			super.onSuccess(response);
			navControl.add(response);
		}
	}

	private class UpdateReceiver extends SaveReceiver
	{

		@Override
		public void onSuccess(NavItemProxy response)
		{
			super.onSuccess(response);
			eventBus.fireEventFromSource(new EntityChange<NavItemProxy>(
					response, WriteOperation.UPDATE), NavItemProxy.class);

		}
	}
	private class SaveReceiver extends Receiver<NavItemProxy>
	{

		@Override
		public void onSuccess(NavItemProxy response)
		{
			view.getEditView().getMessageView()
					.showInfo(SafeHtmlUtils.fromTrustedString("Saved"));
			scheduler.scheduleFixedDelay(new Scheduler.RepeatingCommand()
			{

				@Override
				public boolean execute()
				{
					view.getEditView().getMessageView().clearMessage();
					return false;
				}
			}, 5000);

			selectedObject.setWrappedItem(response);
			NavItemRequest navItemRequest = dataRequestFactory
					.getNavItemRequest();
			navItemRequest.edit(selectedObject.getWrappedItem());
			driver.edit(response, navItemRequest);
			Request<NavItemProxy> save = navItemRequest.save(response);
			save.to(new UpdateReceiver());
		}

	}


}
